package com.ziyueyuwen.xiniao.controller.manage;

import com.alibaba.fastjson.JSONObject;
import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.BaseUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ZyUser;
import com.ziyueyuwen.xiniao.bean.product.ZyCouponDO;
import com.ziyueyuwen.xiniao.enums.CouponStatusEnum;
import com.ziyueyuwen.xiniao.enums.ParamTypeEnum;
import com.ziyueyuwen.xiniao.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.ziyueyuwen.xiniao.base.BaseUtil.*;
import static com.ziyueyuwen.xiniao.enums.ParamTypeEnum.INTEGER_GT_0;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * @author jun 2018/12/20.
 */
@Controller
@RequestMapping("/manage/coupon")
public class ManageCouponController {
    @Autowired
    private CouponService couponService;

    @RequestMapping(value = "/list_coupon.do", method = RequestMethod.POST)
    public void listCoupon(@RequestParam(PARAM_PAGE_NUM) Integer pageNum,
                           @RequestParam(PARAM_PAGE_SIZE) Integer pageSize,
                           @RequestParam(PARAM_COUPON_STATUS) String couponStatus,
                           @RequestParam(value = PARAM_QUERY_DATA, required = false) String queryData,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        HttpResContent<JSONObject> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取优惠券列表");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_PAGE_NUM, pageNum);
            checkParam(INTEGER_GT_0, PARAM_PAGE_SIZE, pageSize);
            checkParam(PARAM_COUPON_STATUS, couponStatus);
            // do.
            CouponStatusEnum couponStatusEnum = CouponStatusEnum.valueOf(couponStatus.toUpperCase());
            JSONObject retObj = couponService.listByManager(pageNum, pageSize, couponStatusEnum, queryData);
            saveSuccessMsgAndData(hrc, retObj);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/add_coupon.do", method = RequestMethod.POST)
    public void addCoupon(@RequestParam(PARAM_COUPON_NAME) String couponName,
                          @RequestParam(PARAM_USER_ACCOUNT) String userAccount,
                          @RequestParam(PARAM_PRICE) Integer price,
                          HttpServletRequest request,
                          HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("添加优惠券");
        try {
            // check.
            BaseUtil.checkParam(PARAM_COUPON_NAME, couponName);
            BaseUtil.checkParam(ParamTypeEnum.ZH_PHONE_NUM, PARAM_USER_ACCOUNT, userAccount);
            BaseUtil.checkParam(INTEGER_GT_0, PARAM_PRICE, price);
            // do.
            ZyUser zyUser = hrc.getUserInfo();
            ZyCouponDO zyCouponDO = new ZyCouponDO();
            zyCouponDO.setCouponName(couponName);
            zyCouponDO.setUserAccount(userAccount);
            zyCouponDO.setPrice(price);
            zyCouponDO.setCreateUserAccount(zyUser.getUserAccount());
            zyCouponDO.setCreateUserName(zyUser.getUserName());
            if (!couponService.add(zyCouponDO)) {
                throw new Exception("保存优惠券入库失败!");
            }
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/stop_coupon.do", method = RequestMethod.POST)
    public void stopCoupon(@RequestParam(PARAM_ID) Integer id,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("停用优惠券");
        try {
            // check.
            BaseUtil.checkParam(INTEGER_GT_0, PARAM_ID, id);
            // do.
            if (!couponService.delete(id)) {
                throw new Exception("优惠券已被使用!");
            }
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

}
